package wechat.rcv.controller;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.CorpWeChatUserDAO;
import com.shrcl.engineerprocess.domain.EngineerProjectJob;
import com.shrcl.engineerprocess.domain.EngineerWorkOrder;
import com.shrcl.engineerprocess.domain.EngineerWorkOrderCriteria;
import com.shrcl.engineerprocess.domain.EngineerWorkOrderStatus;
import edu.fudan.mylang.pf.IObjectFactory;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.IteratorUtils;
import org.apache.log4j.Logger;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import wechat.rcv.controller.ArrayWapper;
import wechat.rcv.controller.WXNotification;

@RestController
@Transactional
@ResponseBody
@RequestMapping("/myworkorder")
@SuppressWarnings("all")
public class GetMyWorkOrder {
  private final static Logger logger = Logger.getLogger(GetMyWorkOrder.class.getName());
  
  @Autowired
  private IObjectFactory entities;
  
  @Autowired
  private CorpWeChatUserDAO corpWeChatUserDAO;
  
  @RequestMapping(method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  public ArrayWapper process(final String wechatid, final Integer page, final Integer pagesize) {
    Object _xblockexpression = null;
    {
      GetMyWorkOrder.logger.info("=========>GetMyWorkOrder.processs<============");
      GetMyWorkOrder.logger.info(("=========>GetMyWorkOrder.wechatid<============" + wechatid));
      GetMyWorkOrder.logger.info(("=========>GetMyWorkOrder.page<============" + page));
      GetMyWorkOrder.logger.info(("=========>GetMyWorkOrder.pagesize<============" + pagesize));
      Object _xtrycatchfinallyexpression = null;
      try {
        EngineerWorkOrderCriteria _createQueryCriteria = this.entities.<EngineerWorkOrder, EngineerWorkOrderCriteria>createQueryCriteria(EngineerWorkOrderCriteria.class);
        final Procedure1<EngineerWorkOrderCriteria> _function = new Procedure1<EngineerWorkOrderCriteria>() {
          @Override
          public void apply(final EngineerWorkOrderCriteria it) {
            it.setStart(Integer.valueOf((((page).intValue() - 1) * (pagesize).intValue())));
            it.setLimit(pagesize);
          }
        };
        final EngineerWorkOrderCriteria c = ObjectExtensions.<EngineerWorkOrderCriteria>operator_doubleArrow(_createQueryCriteria, _function);
        final ArrayWapper ret = new ArrayWapper();
        Collection<EngineerWorkOrder> _list = c.list();
        final Function1<EngineerWorkOrder, WXNotification> _function_1 = new Function1<EngineerWorkOrder, WXNotification>() {
          @Override
          public WXNotification apply(final EngineerWorkOrder o) {
            WXNotification _wXNotification = new WXNotification();
            final Procedure1<WXNotification> _function = new Procedure1<WXNotification>() {
              @Override
              public void apply(final WXNotification it) {
                Long _id = o.getId();
                it.id = _id;
                EngineerProjectJob _projectJob = o.getProjectJob();
                String _jobName = _projectJob.getJobName();
                it.title = _jobName;
                SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date _startDate = o.getStartDate();
                String _format = _simpleDateFormat.format(_startDate);
                String _plus = (_format + " to ");
                SimpleDateFormat _simpleDateFormat_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date _endDate = o.getEndDate();
                String _format_1 = _simpleDateFormat_1.format(_endDate);
                String _plus_1 = (_plus + _format_1);
                it.date = _plus_1;
                EngineerWorkOrderStatus _status = o.getStatus();
                String _plus_2 = ("=========>GetMyWorkOrder.Status<============" + _status);
                InputOutput.<String>println(_plus_2);
                EngineerWorkOrderStatus _status_1 = o.getStatus();
                boolean _equals = Objects.equal(_status_1, EngineerWorkOrderStatus.executing);
                if (_equals) {
                  it.isFinished = Boolean.valueOf(false);
                } else {
                  it.isFinished = Boolean.valueOf(true);
                }
              }
            };
            return ObjectExtensions.<WXNotification>operator_doubleArrow(_wXNotification, _function);
          }
        };
        final Iterable<WXNotification> result = IterableExtensions.<EngineerWorkOrder, WXNotification>map(_list, _function_1);
        Iterator<WXNotification> _iterator = result.iterator();
        List _list_1 = IteratorUtils.toList(_iterator);
        ret.result = _list_1;
        int _size = ret.result.size();
        ret.count = Integer.valueOf(_size);
        InputOutput.<String>println(("*******GetMyWorkOrder.result.size******" + ret.count));
        InputOutput.<String>println("*******GetMyWorkOrder.response******");
        return ret;
      } catch (final Throwable _t) {
        if (_t instanceof Exception) {
          final Exception e = (Exception)_t;
          _xtrycatchfinallyexpression = null;
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
      _xblockexpression = _xtrycatchfinallyexpression;
    }
    return ((ArrayWapper)_xblockexpression);
  }
}
